# 导入Web测试驱动库
from selenium import webdriver
# 导入交互操作ActionChains(鼠标指针悬停、滚动、拖放等操作)
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.firefox.webdriver import WebDriver
from selenium.webdriver.common.by import By
from selenium.common import exceptions as ex
from time import sleep, time
import selenium.webdriver.support.ui as ui

# 选择需要测试的浏览器
driver: WebDriver = webdriver.Firefox(executable_path='geckodriver')
# driver: WebDriver = webdriver.Chrome(executable_path='chromedriver')
# 等待加载超时时间
driver.implicitly_wait(20)
# 打开博学谷主页
driver.get("https://www.boxuegu.com/")
sleep(2)
# 获取登录按钮ID进行单机操作
# driver.find_element(By.XPATH, "/html/body/div[1]/div/div/header/nav/div[2]/div[3]/div[1]/span[1]").click()
driver.find_element(By.CLASS_NAME, "login-btn").click()
# 再次点击登录按钮
driver.find_element(By.XPATH, "/html/body/div/div/div/div/div[1]/div/p[1]/span").click()
# 选定账号输入框
driver.find_element(By.XPATH, "/html/body/div/div/div/div/div[1]/div/form/div[1]/div/div/input")
# 输入账号
driver.find_element(By.XPATH, "/html/body/div/div/div/div/div[1]/div/form/div[1]/div/div/input").send_keys(
    '18907594441')
# 选定密码输入框
driver.find_element(By.XPATH, "/html/body/div/div/div/div/div[1]/div/form/div[2]/div/div[1]/input")
# 输入密码
driver.find_element(By.XPATH, "/html/body/div/div/div/div/div[1]/div/form/div[2]/div/div[1]/input").send_keys(
    'clgr2547.')

# 单机【登录】按钮
driver.find_element(By.XPATH, "/html/body/div/div/div/div/div[1]/div/div[2]/button").click()

sleep(2)
# 点击 "免费学"
driver.find_element(By.XPATH, "/html/body/div[1]/div/div/header/nav/div[1]/ul/li[5]").click()

sleep(1)
# 模拟点击软件测试


list_windows = driver.window_handles
driver.switch_to.window(list_windows[1]) #list_windows 存储了上一步中获取的窗口

sleep(1)
driver.get("https://ke.boxuegu.com/?m=5")
# st = driver.find_element(By.XPATH,"/html[1]/body[1]/div[1]/content[1]/div[1]/section[1]/div[1]/div[1]/div[1]/ul[1]/li[1]/ul[1]/li[12]");
# print(st.tag_name)

sleep(1)

# 点击黑马自动化测试视频
driver.find_element(By.XPATH,"/html[1]/body[1]/div[1]/content[1]/div[1]/section[2]/div[1]/div[1]/div[1]/div[1]/div[1]/div[1]/div[1]/a[1]/img[1]").click()
sleep(1)
list_windows = driver.window_handles
driver.switch_to.window(list_windows[2]) #list_windows 存储了上一步中获取的窗口

top = driver.find_element(By.XPATH,"/html[1]/body[1]/div[1]/content[1]/div[1]/div[1]/div[2]/div[2]/div[3]/div[1]/div[1]/div[2]/div[1]/div[1]/div[1]")

# # 获取02用例梳理及测试环境
ts02 = driver.find_element(By.CSS_SELECTOR,"content.content:nth-child(2) div.detailsPage div.detailsPage-box div.content div.container.detail-main div.row.course-cont:nth-child(3) div.course-left div.couse-catalog-box div.catalog-cont div.catalog-list-box div.catalog-list:nth-child(1) ul.catalog-ul > li.Unlock:nth-child(3)")
sleep(1)
# 滑动滚动条到某个指定的元素
moveItem = driver.execute_script("arguments[0].scrollIntoView();",top)
ActionChains(driver).move_to_element(ts02).perform()

sleep(1)
# 点击开始学习
driver.find_element(By.XPATH,"/html[1]/body[1]/div[1]/content[1]/div[1]/div[1]/div[2]/div[2]/div[3]/div[1]/div[1]/div[2]/div[1]/div[1]/ul[1]/li[3]/div[2]/div[1]").click()

# 获取视频标题
tag = driver.find_element(By.XPATH,"/html/body/div/div[2]/content/div[2]/div[1]/div/div[1]/div/ul/li[3]/ol/li/h2")

# 滚动到视频标题 使视频播放显示处理
driver.execute_script("arguments[0].scrollIntoView();",tag)
sleep(1)
play_btn = driver.find_element(By.XPATH,"/html/body/div/div[2]/content/div[2]/div[1]/div/div[1]/div/ul/li[4]/ol/li/div/div/div/div/div/div[1]/div")
sleep(1)
# 悬停
ActionChains(driver).move_to_element(play_btn).perform()
play_btn.click()



